import type { MockMethod } from 'vite-plugin-mock'
import { UserTableRows } from './user'
import { errorReturn, successReturn } from './utils'

export default [
  {
    url: '/mock-api/auth/login',
    method: 'post',
    response({ body }) {
      const { username, password } = body
      const user = UserTableRows.find((row) => row.username === username)
      if (!user) {
        return errorReturn(1, '没有该用户')
      }
      if (user.password !== password) {
        return errorReturn(2, '密码错误')
      }
      return successReturn({
        id: user.id,
        username: user.username,
        phone: user.phone,
        email: user.email,
        avatar: user.avatar,
        token: 'token',
        roles: user.roles,
        permissions: user.permissions,
        root: user.username === 'root'
      })
    }
  }
] as MockMethod[]
